﻿/*
 * LINQ to LDAP
 * http://linqtoldap.codeplex.com/
 * 
 * Copyright Alan Hatter (C) 2010-2012
 * alan.hatter@gmail.com - http://hattercoding.blogspot.com/
 * 
 * This project is subject to licensing restrictions. Visit http://linqtoldap.codeplex.com/license for more information.
 */

using System;
using System.Collections.Generic;
using System.Linq;
using LinqToLdap.Helpers;

namespace LinqToLdap.Mapping
{
    internal class AnonymousObjectMapping<T> : ObjectMapping where T : class
    {
        private readonly CtorWithParams<T> _constructor;

        public AnonymousObjectMapping(string namingContext,
            IEnumerable<IPropertyMapping> propertyMappings, string objectCategory, IEnumerable<string> objectClass)
            : base(namingContext, propertyMappings, objectCategory, objectClass)
        {
            
            _constructor = DelegateBuilder.BuildCtorWithParams<T>(typeof(T).GetConstructors().First());
        }

        public override bool IsForAnonymousType { get { return true; } }

        public override Type Type
        {
            get { return typeof(T); }
        }

        public override object Create(object[] parameters = null)
        {
            T instance = _constructor(parameters);
            return instance;
        }
    }
}
